import request from '@/utils/request'

const path = locale['securityApi']

/** ***************************用户相关*************************************/
/**
 * 获取登录相关的用户信息
 */
export function getLoggedInUser () {
  return request({
    url: path + `/security/users/getLoggedInUser`,
    method: 'get'
  })
}

/**
 * 设置语言
 * @param userId
 * @param locale
 */
export function setlocale (userId, locale) {
  return request({
    url: path + `/security/users/${userId}/locale/${locale}`,
    method: 'put'
  })
}

/**
 * 用户退出
 */
export function logoutUAA () {
  return request({
    url: path + `/uaa/logout`,
    method: 'post'
  })
}

export function logout () {
  return request({
    url: path + `/logout`,
    method: 'post'
  })
}

/** **************************************应用权限相关*************************************/
/**
 * 根据code获取应用信息
 * @param _params
 */
export function getByApplicationCode (_params) {
  return request({
    url: path + `/security/applications`,
    method: 'get',
    params: _params
  })
}

/**
 * 查询某租户下某应用系统的某用户对应功能菜单
 * @param tenantId
 * @param applicationId
 * @param userId
 */
export function getMenusByUserId (tenantId, applicationId, userId) {
  return request({
    url: path + `/security/tenants/${tenantId}/applications/${applicationId}/users/${userId}/menus`,
    method: 'get'
  })
}

/**
 * 查询某租户下某应用系统所有权限列表，标记出该用户拥有的权限，同时区分其权限来自于用户组还是用户
 * @param tenantId
 * @param applicationId
 * @param userId
 */
export function getUserAccessesMap (tenantId, applicationId, userId) {
  return request({
    url: path + `/security/tenants/${tenantId}/applications/${applicationId}/users/${userId}/accessesMap`,
    method: 'get'
  })
}

/**
 * 查询某租户可访问的应用系统列表
 * @param tenantId
 * @param userId
 */
export function getAppsByTenantIdAndUserId (tenantId, userId) {
  return request({
    url: path + `/security/tenants/${tenantId}/users/${userId}/applications`,
    method: 'get'
  })
}

/**
 * 获取应用系统相关信息
 * @param tenantId
 * @param userId
 * @param code
 */
export function getAppByTenantIdAndUserIdAndCode (tenantId, userId, code) {
  return request({
    url: path + `/security/tenants/${tenantId}/users/${userId}/applicationCode/${code}`,
    method: 'get'
  })
}

/**
 * 查询某租户下某应用系统的用户组列表
 * @param tenantId
 * @param applicationId
 */
export function getGroupsBy (tenantId, applicationId) {
  return request({
    url: path + `/security/tenants/${tenantId}/applications/${applicationId}/groups`,
    method: 'get'
  })
}


/** *************************************其它*************************************/

/**
 * 修改密码
 * @param userId
 * @param data
 */
export function passChange (userId, data) {
  return request({
    url: path + `/security/users/${userId}/passwordChange`,
    method: 'put',
    params: data
  })
}

/**
 * 获取应用系统相关地址信息
 * @param params_
 */
export function getAppServiceUrl (params_) {
  return request({
    url: path + `/security/applications/serviceUrl`,
    method: 'get',
    params: params_
  })
}

/**
 * 获取用户列表
 * @param _params
 */
export function getUserByPage (_params) {
  return request({
    url: path + `/security/users/pagination`,
    method: 'get',
    params: _params
  })
}
